﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace FRW.Common.Extensions
{
    public class EnumDescriptionAttribute : Attribute
    {
        public string Text;

        public EnumDescriptionAttribute(string text)
        {
            Text = text;
        }

    }

    public static class EnumExtensions
    {
        public static string ToDescription(this Enum en) //ext method
        {

            Type type = en.GetType();

            MemberInfo[] memInfo = type.GetMember(en.ToString());

            if (memInfo != null && memInfo.Length > 0)
            {

                object[] attrs = memInfo[0].GetCustomAttributes( typeof(EnumDescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0)

                    return ((EnumDescriptionAttribute)attrs[0]).Text;
            }

            return en.ToString();

        }

    }
}
